package com.jh.ul;

import Dao.MysqlOperate;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

public class User_Register extends JFrame implements ActionListener {

    public User_Register() {
        setJFrame();

        this.setVisible(true);
    }

    //设计组件对象，并用于监控
    JTextField name=null;
    JPasswordField password=null;
    JPasswordField cx_password=null;
    JTextField employer_id=null;
    JTextField position=null;

    JButton zhiwei_yanzheng=null;
    JButton register=null;



    //职位验证检查器
    boolean bool=false;

    private void setJFrame() {
        this.setSize(400,500);
        this.setTitle("注册界面");

        //固定尺寸
        this.setResizable(false);

//        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        //取消组件默认布局
        this.setLayout(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //图片加载
        name = new JTextField();
        name.setBounds(160,163,150,22);
        this.getContentPane().add(name);

        password = new JPasswordField();
        password.setBounds(160,202,150,22);
        this.getContentPane().add(password);

        cx_password = new JPasswordField();
        cx_password.setBounds(160,240,150,22);
        this.getContentPane().add(cx_password);

        employer_id=new JTextField();
        employer_id.setBounds(160,280,150,22);
        this.getContentPane().add(employer_id);

        position=new JTextField();
        position.setBounds(160,320,70,22);
        this.getContentPane().add(position);

        zhiwei_yanzheng=new JButton(new ImageIcon("resource\\职位验证.jpg"));
        zhiwei_yanzheng.setBounds(240,320,87,40);
        this.getContentPane().add(zhiwei_yanzheng);

        register=new JButton(new ImageIcon("resource\\注册.jpg"));
        register.setBounds(155,390,87,40);
        this.getContentPane().add(register);


        JLabel jLabel = new JLabel(new ImageIcon("resource\\注册界面.jpg"));
        jLabel.setBounds(0,0,400,500);
        this.getContentPane().add(jLabel);

        //鼠标监听
        register.addActionListener(this);

    }


    @Override
    public void actionPerformed(ActionEvent e) {


        Object source=e.getSource();
        //获取填入的信息
        String user_name=name.getText();
        String user_password= Arrays.toString(password.getPassword());
        String user_cx_password= Arrays.toString(cx_password.getPassword());
        String user_employId= employer_id.getText();
        String user_position=position.getText();
        if(user_name == null || user_name.trim().isEmpty()){
          new error("用户名不能为空");
          return;
        }
        if(user_password.trim().isEmpty()){
            new error("密码不能为空");
            return;
        }
        if(user_cx_password.trim().isEmpty()){
            new error("密码不能为空");
            return;
        }
        if(user_employId == null || user_employId.trim().isEmpty()){
            new error("员工编号不能为空");
            return;
        }
        if(user_position == null || user_position.trim().isEmpty()){
            new error("职位不能为空");
            return;
        }







        if (source.equals(zhiwei_yanzheng)){
            //职位验证
            if (bool){
                new error("已经验证！");
                return;
            }
            ArrayList<Object[]>arr_em=null;
            try {
                arr_em=MysqlOperate.find("employee_info",null,null);
            } catch (SQLException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
            for (int i = 0; i < arr_em.size() ; i++) {
                if (arr_em.get(i)[0].equals(user_employId)&&arr_em.get(i)[1].equals(user_position)){
                    new error("职位验证成功！");
                    bool=true;
                    return;
                }
            }

        } else if (source.equals(register)) {
            //职位验证
            if (!bool){
                new error("请先通过职位验证进行注册");
                return;
            }
            //检查重名
            //连接数据库
            ArrayList<Object[]>arr_user=null;
            try {
                arr_user= MysqlOperate.find("user_info",null,null);
            } catch (SQLException | ClassNotFoundException ex) {
                throw new RuntimeException(ex);
            }
            for (int i = 0; i <arr_user.size() ; i++) {
                if (arr_user.get(i)[0].equals(user_name)){
                    new error("用户名重复，请重新输入");
                    name.setText("");
                    return;
                }
            }

        }

        //文本监听
        employer_id.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                textChanged(e);

            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                textChanged(e);

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                textChanged(e);
            }
            private void textChanged(DocumentEvent e) {
                bool=false;

            }
        });

    }
}
